package com.dan.inter.service.impl;

import java.util.Map;

import org.json.JSONObject;
import org.springframework.stereotype.Service;

import com.dan.inter.service.TuLingApiService;
import com.dan.inter.util.tuling.Aes;
import com.dan.inter.util.tuling.Md5;
import com.dan.inter.util.tuling.PostServer;


@Service
public class TuLingApiServiceImpl implements TuLingApiService {

	public String getTulingResult(Map<String,String> contentMap) throws Exception {
		// 图灵网站上的secret
		String secret = contentMap.get("secret");
		// 图灵网站上的apiKey
		String apiKey = contentMap.get("apiKey");
		
		String content = contentMap.get("content");
		// 待加密的json数据
		String data = "{\"key\":\"" + apiKey + "\",\"info\":\"" + content + "\"}";
		// 获取时间戳
		String timestamp = String.valueOf(System.currentTimeMillis());

		// 生成密钥
		String keyParam = secret + timestamp + apiKey;
		String key = Md5.MD5(keyParam);

		// 加密
		Aes mc = new Aes(key);
		data = mc.encrypt(data);

		// 封装请求参数
		JSONObject json = new JSONObject();
		json.put("key", apiKey);
		json.put("timestamp", timestamp);
		json.put("data", data);
		// 请求图灵api
		String result = PostServer.SendPost(json.toString(), "http://www.tuling123.com/openapi/api");
		return result;
	}

}
